package BaseDatos;

import Modelo.Aula;
import Modelo.Grupo;
import Modelo.Profesor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class GestorAula {

    private String URL_Servidor = "localhost";
    private static final String BASE_DATOS = "ADEHO";
    private static final String LOGIN = "root";
    private static final String PASSWORD = "root";
 
    private static final String CMD_LISTAR = "SELECT * FROM aula";
    private static final String CMD_REGISTRAR_AULA = "INSERT INTO aula VALUES(?,?)";
    private static final String CMD_OBTENER_AULA = "SELECT * FROM aula WHERE idAula=?";
    private static final String CMD_BORRAR_AULA = "DELETE FROM aula where idAula=?";


    public ArrayList<Aula> ListarAulas() {
        ArrayList<Aula> aulas = new ArrayList();
        GestorBD bd = null;
        try {
            bd = GestorBD.obtenerInstancia(URL_Servidor);
            Connection cnx = bd.obtenerConexion(BASE_DATOS, LOGIN, PASSWORD);
            Statement stm = cnx.createStatement();
            ResultSet rs = stm.executeQuery(CMD_LISTAR);
            while (rs.next()) {
                Aula a = new Aula(rs.getString(1),rs.getString(2));
                aulas.add(a);
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
        } finally {
            bd.cerrarConexion();
        }
        return aulas;
     }

    public boolean registrarAula(Aula nAula) {
        boolean registrada = false;

        int registrosActualizados = 0;
        GestorBD bd = null;
        try {
            bd = GestorBD.obtenerInstancia(URL_Servidor);
            Connection cnx
                    = bd.obtenerConexion(BASE_DATOS, LOGIN, PASSWORD);

            PreparedStatement stm = cnx.prepareStatement(CMD_REGISTRAR_AULA);
            stm.clearParameters();
            stm.setString(1, nAula.getIdAula());
            stm.setString(2, nAula.getDescripcion());
            registrosActualizados = stm.executeUpdate();
            registrada = registrosActualizados == 1;
            stm.close();

        } catch (Exception e) {
            e.printStackTrace(System.err);
        } finally {
            bd.cerrarConexion();
        }
        return registrada;
    }

    public boolean borrarAula(String id) {
        boolean borrado = false;
        int exito = 0;
        GestorBD bd = null;
        try {
            bd = GestorBD.obtenerInstancia(URL_Servidor);
            Connection cnx
                    = bd.obtenerConexion(BASE_DATOS, LOGIN, PASSWORD);
            PreparedStatement stm = cnx.prepareStatement(CMD_BORRAR_AULA);
            stm.clearParameters();
            stm.setString(1, id);

            exito = stm.executeUpdate();
            if (exito != 0) {
                borrado = true;
            }
            stm.close();

        } catch (Exception e) {
            e.printStackTrace(System.err);
        } finally {
            bd.cerrarConexion();
        }
        return borrado;
    }

    public Aula obtenerAula(String idAula) {
        Aula a = null;

        GestorBD bd = null;
        try {
            bd = GestorBD.obtenerInstancia(URL_Servidor);
            Connection cnx
                    = bd.obtenerConexion(BASE_DATOS, LOGIN, PASSWORD);

            PreparedStatement stm = cnx.prepareStatement(CMD_OBTENER_AULA);
            stm.clearParameters();
            stm.setString(1, idAula);

            ResultSet rs = stm.executeQuery();

            a = new Aula(rs.getString(1),rs.getString(2));
            stm.close();

        } catch (Exception e) {
            e.printStackTrace(System.err);
        } finally {
            bd.cerrarConexion();
        }
        return a;
    }
}
